from warnings import showwarning

import time
import threading
pygame.init()
s_w=720
s_h=660
screen = pygame.display.set_mode((s_w,s_h))


def get_imgae(image,x,y,w,h,scale=1,color_key=(255,255,255)):
    im=pygame.Surface((w,h))
    im.blit(image,(0,0),(x,y,w,h))
    if color_key is not None:
        im.set_colorkey(color_key)
    im=pygame.transform.scale(im,(int(w*scale),int(h*scale)))
    return im


player1_imgs={
    'static_r':get_imgae(pygame.image.load('./player.png'),371,84,135,80,0.6),
    'static_l':get_imgae(pygame.image.load('./player.png'),678,80,133,177,0.6),
    'mov_l':get_imgae(pygame.image.load('./player.png'),1297,77,142,167,0.6),
    'mov_r':get_imgae(pygame.image.load('./player.png'),1038,84,131,167,0.6)
}
player2_imgs={
    'static_r':get_imgae(pygame.image.load('./player.png'),725,396,113,164,0.6),
    'static_l':get_imgae(pygame.image.load('./player.png'),348,391,110,169,0.6),
    'mov_l':get_imgae(pygame.image.load('./player.png'),1001,393,114,145,0.6),
    'mov_r':get_imgae(pygame.image.load('./player.png'),1388,391,106,141,0.6)
}

player_imgs=[player1_imgs,player2_imgs]




class Ball:
    def __init__(self):

        self.g=1

        

        pass

    def move(self):

        pass

    def is_ball_out(self):
        pass

    def is_ball_stop(slef):
        pass


class agent:
    def __init__(self,type=1):
        self.step=10
        self.x=0
        self.y=0
        self.side=0
        self.player=pygame.sprite.Sprite()
        self.type=type
        self.pose=player_imgs[self.type-1]
        self.ymin=-10
        self.ymax=550
        self.xmin=0
        self.xmax=s_w-30
        self.pose=[self.pose['static_l'],self.pose['static_r'],self.pose['mov_l'],self.pose['mov_r']]
        self.left_pose=self.pose[:2]
        self.right_pose=self.pose[2:]
        self.pose=self.left_pose
        self.init()
        self.pose_index=0
        self.l_actions=[7,4,9,15,12,17]
        self.r_actions=[6,3,8,14,11,16]
        self.act=0

        pass

    def init(self):
        self.player.image=self.pose[0]
        self.player.rect=self.player.image.get_rect()
        if self.type==1:
            self.ymin=240
            self.ymax=510
            self.player.rect.x=s_w/2
            self.player.rect.y=self.ymax
            
        else:
           self.ymin=-10
           self.ymax=210
           self.player.rect.x=s_w/2
           self.player.rect.y=self.ymin
        self.x=self.player.rect.x
        self.y=self.player.rect.y

        print('init',self.x,self.y)
        print('inited %d'%self.type)

    def submit_action(self,act):
        '''
        提交动作
        '''
        
        
        pass

    def get_player(self):
        return self.player

    def move(self,delta_x=0,delta_y=0):
        self.player.rect.x+=delta_x
        if self.player.rect.x<self.xmin:
            self.player.rect.x=self.xmin
        if self.player.rect.x>self.xmax:
            self.player.rext.x=self.xmax

        self.player.rect.y+=delta_y
        if self.player.rect.y<self.ymin:
            self.player.rect.y=self.ymin
        if self.player.rect.y>self.ymax:
            self.player.rect.y=self.ymax
        self.x=self.player.rect.x
        self.y=self.player.rect.y



        




class human_agent(agent):
    def __init__(self,type=1):
        super(human_agent,self).__init__(type=type)
         
        self.w=0
        self.s=0
        self.a=0
        self.d=0
        self.j=0
        self.act=0
        self.act_dic={
            0:0,
            1:1,
            16:2,
            2:3,
            4:4,
            8:5,
            18:6,
            20:7,
            10:8,
            12:9,
            17:10,
            3:11,
            5:12,
            9:13,
            19:14,
            21:15,
            11:16,
            13:17
        }
        # self.op_th=threading.Thread(target=self.operate_thread) 
        # self.op_th.start()

        self.id=0
    def submit_action(self):

        pass

    def take_action(self):


        if self.act in self.l_actions:
            if not self.side==0:
                self.side=0
                self.pose_index=0
                self.pose=self.left_pose
        if self.act in self.r_actions:
            if not self.side==1:
                self.side=1
                self.pose_index=1
                self.pose=self.right_pose
        
        if self.act==7:
            self.move(-self.step,-self.step)
        if self.act==4:
            self.move(-self.step,0)
        if self.act==9:
            self.move(-self.step,self.step)
        if self.act==15:
            self.move(-self.step,-self.step)
            self.submit_action()
        if self.act==12:
            self.move(-self.step,-self.step)
            self.submit_action()
        if self.act==17:
            self.move(-self.step,-self.step)
            self.submit_action()
        if self.act==2:
            self.move(0,-self.step)
        if self.act==6:
            self.move(self.step,self.step)
        if self.act==3:
            self.move(self.step,0)
        if self.act==8:
            self.move(self.step,self.step)
        if self.act==5:
            self.move(0,self.step)
        
        if self.act==10:
            self.move(0,-self.step)
            self.submit_action()
        if self.act==14:
            self.move(self.step,self.step)
            self.submit_action()
        if self.act==11:
            self.move(self.step,0)
            self.submit_action()
        if self.act==16:
            self.move(self.step,self.step)
            self.submit_action()
        if self.act==13:
            self.move(0,self.step)
            self.submit_action()
        
        if self.act==1:
            self.submit_action()
        


        
        
        pass

    def get_player(self):
        return self.player


    def operate_thread(self):
        while True:
            for event in pygame.event.get():
                if event.type==pygame.KEYDOWN:
                    keys=pygame.key.get_pressed()
                    if keys[pygame.K_w]:
                        self.w=1
                        
                    if keys[pygame.K_a]:
                        print('down',self.id)
                        self.id+=1
                        self.a=1
                    if keys[pygame.K_s]:
                        self.s=1
                    if keys[pygame.K_d]:
                        self.d=1
                    if keys[pygame.K_j]:
                        self.j=1
                    
                    # self.set_key_state()
            # for event in pygame.event.get():
                if event.type==pygame.KEYUP:
                    keys1=pygame.key.get_pressed()
                    print('release')
                    if keys1[pygame.K_w]:
                        self.w=0
                    if keys1[pygame.K_a]:
                        
                        self.a=0
                    if keys1[pygame.K_s]:
                        self.s=0
                    if keys1[pygame.K_d]:
                        self.d=0
                    if keys1[pygame.K_j]:
                        self.j=0

            

    def set_key_state(self,w,s,a,d,j):
        self.w=w
        self.s=s
        self.a=a
        self.d=d
        self.j=j
        act=(self.w*16+self.s*8+self.a*4+self.d*2+self.j)
        
        self.act=self.act_dic[act] if act in self.act_dic.keys() else 0
        self.take_action()
        self.w=0
        self.s=0
        self.a=0
        self.d=0
        self.j=0
        self.act=0
        print(self.w,self.s,self.a,self.d)


    def change_pose(self):
        self.pose_index%=2
        self.player.image=self.pose[self.pose_index]
        self.pose_index+=1


        
        


if __name__=='__main__':

    bgpic=pygame.image.load('./field.png')
    bgpic=get_imgae(bgpic,396,10,708,794,color_key=None)
    bgpic=pygame.transform.scale(bgpic,((s_w,s_h)))
    player_group=pygame.sprite.Group()

    p1=human_agent(1)
    # p2=agent(2)


    player_group.add(p1.get_player())
    # player_group.add(p2.get_player())

    done = False
    w=0
    s=0
    a=0
    d=0
    j=0
    while not done:
        p1.take_action()
        for event in pygame.event.get():
                if event.type==pygame.KEYDOWN:
                    keys=pygame.key.get_pressed()
                    if keys[pygame.K_w]:
                        w=1
                    if keys[pygame.K_a]:                
                        a=1
                    if keys[pygame.K_s]:
                        s=1
                    if keys[pygame.K_d]:
                        d=1
                    if keys[pygame.K_j]:
                        j=1   
                if event.type==pygame.KEYUP:
                    keys1=pygame.key.get_pressed()
                    if keys1[pygame.K_w]:
                        w=0
                    if keys1[pygame.K_a]:
                        
                        a=0
                    if keys1[pygame.K_s]:
                        s=0
                    if keys1[pygame.K_d]:
                        d=0
                    if keys1[pygame.K_j]:
                        j=0
        p1.set_key_state(w,s,a,d,j)
        w=0
        s=0
        a=0
        d=0
        j=0
        screen.blit(bgpic,(0,0))
        pygame.display.flip()
        player_group.draw(screen)
        pygame.display.update()
        time.sleep(0.1)
    